using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using MyFrame.Event;
using MyFrame.Pool;
using Platform.Skill.Core;
using UnityEngine;
using Plugin.Tool;

namespace Platform.Skill.Deployer
{
    /// <summary>
    /// 黑洞，复合技能
    /// </summary>
    public class BlackholeSkillDeployer : SkillDeployer
    {
        private SkillMgr skillMgr;

        public float growSpeed = 3;
        public float shrinkSpeed = 6;

        public List<Transform> targets;

        private bool canAttack = true;

        private float cloneAttackTimer;

        void Update()
        {
            cloneAttackTimer += Time.deltaTime;
        }

        protected override void OnTriggerEnter2D(Collider2D other)
        {
            if (other.gameObject.layer == LayerMask.NameToLayer("Enemy"))
            {
                other.GetComponent<Enemy>()?.FreezeTime(true);
                targets.Add(other.transform);
            }
        }

        protected override void OnTriggerExit2D(Collider2D other)
        {
            other.GetComponent<Enemy>()?.FreezeTime(false);
        }

        void OnEnable()
        {
            GameMgr.Instance.player.attackAction.performed += Attack;
        }

        void OnDisable()
        {
            GameMgr.Instance.player.attackAction.performed -= Attack;
        }

        public override void DeploySkill()
        {
            base.DeploySkill();
            transform
                .DOScale(new Vector2(skillData.range, skillData.range), growSpeed)
                .SetEase(Ease.InOutQuad);
            canAttack = true;

            skillMgr = skillData.owner.GetComponent<SkillMgr>();

            ZTimer.SetTimeout(
                skillData.durationTime,
                () =>
                {
                    transform
                        .DOScale(Vector2.one, shrinkSpeed)
                        .SetEase(Ease.InOutQuad)
                        .OnComplete(() =>
                        {
                            // 技能结束
                            PoolMgr.Instance.Release(this.gameObject);

                            //不好用事件解耦  因为敌人也有技能
                            skillData
                                .owner.GetComponent<Entity>()
                                .ChangeState(Entity.States.AirState);
                            skillData.owner.GetComponent<Entity>().MakeTransparent(false);
                        });
                    canAttack = false;
                }
            );
        }

        private void Attack(UnityEngine.InputSystem.InputAction.CallbackContext context)
        {
            if (!canAttack || targets.Count == 0 || cloneAttackTimer < skillData.attackInterval)
                return;

            cloneAttackTimer = 0;

            skillData.combo++;
            if (skillData.combo >= skillData.comboCount)
            {
                skillData.combo = 0;
                canAttack = false;
            }

            //在随机敌人处生成克隆
            var offsetX = UnityEngine.Random.value > 0.5 ? -1 : 1;
            int randomIndex = UnityEngine.Random.Range(0, targets.Count);
            var target = targets[randomIndex];
            var realPos = target.GetComponent<Enemy>().GetRealPos();
            skillMgr.UseSkill(SkillEnum.Clone, new Vector3(realPos.x + offsetX, realPos.y, 0));

            //玩家消失
            skillData.owner.GetComponent<Entity>().MakeTransparent(true);
        }
    }
}
